package com.bree.erp.service.stateless;

import java.util.Collection;
import java.util.Date;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import com.bree.erp.entity.PrAudit;
import com.bree.erp.entity.Project;
import com.bree.erp.entity.PurchaseRequisition;
import com.bree.erp.entity.Status;
import com.bree.erp.entity.User;
import com.bree.erp.service.PurchaseRequisitionService;

@Stateless
public class PurchaseRequisitionServiceBean implements
		PurchaseRequisitionService {


	@PersistenceContext()
	private EntityManager em;

	public PurchaseRequisitionServiceBean() {

	}

	public PurchaseRequisitionServiceBean(EntityManager em) {
		this.em = em;
	}

	@Override
	public Collection<PurchaseRequisition> list(String namedQuery, User user) {

		TypedQuery<PurchaseRequisition> query = em.createNamedQuery(
				namedQuery, PurchaseRequisition.class)
				.setParameter("user", user);
		
		return query.getResultList();
	}

	@Override
	public Collection<PurchaseRequisition> list() {

		TypedQuery<PurchaseRequisition> query = em.createNamedQuery(
				PurchaseRequisition.QUERY_FIND_ALL_PURCHASEREQUISTION, PurchaseRequisition.class);
		
		return query.getResultList();
	}

	@Override
	public PurchaseRequisition create(User user, Project project, String description) {
		
		PurchaseRequisition pr = new PurchaseRequisition();
		
		pr.setProject(project);
		pr.setDescription(description);
		pr.setStatus(Status.OPEN);
		
		pr.auditCreate(user);
		
		em.persist(pr);
		
		
		return pr;
	}

	@Override
	public void delete(PurchaseRequisition purchaseRequisition) {

		em.remove(purchaseRequisition);
		
	}

	@Override
	public PurchaseRequisition read(Long id, boolean refresh) {
		
		PurchaseRequisition entity = em.find(PurchaseRequisition.class, id);
		if(refresh && entity != null) {
			em.refresh(entity);
		}
		
		return entity;
	}

	@Override
	public PurchaseRequisition read(Long id) {
		
		return read(id, false);
	}

	@Override
	public PurchaseRequisition update(Long id, Project project,
			String description, User user) {
		
		PurchaseRequisition entity = read(id);
		
		entity.setProject(project);
		entity.setDescription(description);
	
		entity.auditUpdate(user);
		
		return entity;
	}

	@Override
	public PurchaseRequisition update(Long id, Long projectId,
			String description, String userId) {
		Project project = em.find(Project.class, projectId);
		User user = em.find(User.class, userId);
		
		return update(id, project, description, user);
	}

	@Override
	public void delete(Long id) {
		PurchaseRequisition entity = read(id);
		
		em.remove(entity);
	}

	@Override
	public PurchaseRequisition submit(Long id) {
		PurchaseRequisition entity = read(id);
		
		entity.setRequisitionDate(new Date());
		entity.setStatus(Status.PENDING);
		
		return entity;
	}

	@Override
	public PurchaseRequisition reopen(Long id, String userId) {
		PurchaseRequisition entity = read(id);
		User user = em.find(User.class, userId);
		entity.setStatus(Status.OPEN);
		entity.auditUpdate(user);
		return entity;
	}

	@Override
	public PurchaseRequisition approve(Long id, String userId) {
		PurchaseRequisition entity = read(id);
		User user = em.find(User.class, userId);
		entity.setStatus(Status.APPROVED);
		entity.auditApprove(user);
		return entity;
	}

	@Override
	public Collection<PurchaseRequisition> list(Status status) {

		TypedQuery<PurchaseRequisition> query = em.createNamedQuery(
				PurchaseRequisition.QUERY_FIND_PURCHASEREQUISTION_PER_STATUS, PurchaseRequisition.class)
				.setParameter("status", status);
		
		return query.getResultList();
	}

}
